package com.fish.admin.module.auth.strategy;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.fish.admin.annotation.UserTypeAnnotation;
import com.fish.admin.module.auth.domain.entity.AuthInfo;
import com.fish.admin.module.auth.manager.AuthInfoManager;
import com.fish.admin.module.system.domain.entity.SysUser;
import com.fish.admin.module.system.manager.SysRoleManager;
import com.fish.admin.module.system.manager.SysUserManager;
import com.fish.common.core.base.AuthenticationRequest;
import com.fish.common.core.base.AuthenticationResponse;
import com.fish.common.core.base.LoginAppUser;
import com.fish.common.core.base.Role;
import com.fish.common.core.context.UserContextManager;
import com.fish.common.core.enums.EnabledEnum;
import com.fish.common.core.enums.SystemCode;
import com.fish.common.core.enums.UserTypeEnum;
import com.fish.common.support.util.AssertUtils;
import com.fish.common.support.util.BeanHelpers;
import com.fish.common.support.util.EnumUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Optional;

@Component
@RequiredArgsConstructor
@UserTypeAnnotation(UserTypeEnum.SYSTEM)
public class SystemAuthStrategy implements AuthStrategy{

    private final AuthInfoManager authInfoManager;

    private final SysUserManager sysUserManager;

    private final SysRoleManager sysRoleManager;
    @Override
    public AuthenticationResponse authenticate(AuthenticationRequest authentication, UserTypeEnum userType) {
        AuthInfo authInfo = authInfoManager.getByAccount(authentication.getAccount());
        AssertUtils.notNull(authInfo, SystemCode.INVALID_USERNAME_PASSWORD);
        AssertUtils.isTrue(authInfo.getSecret().equals(authentication.getPassword()),SystemCode.INVALID_USERNAME_PASSWORD);
        AssertUtils.isTrue(EnumUtils.equals(authInfo.getEnabled(), EnabledEnum.YES), "用户已禁用，请联系管理员!");
        AssertUtils.isTrue(authInfo.getUserType().equals(userType.getValue()),"非法用户禁止登陆!");
        SysUser sysUser = sysUserManager.getById(authInfo.getUserId());
        AssertUtils.notNull(sysUser, "用户不存在!");
        LoginAppUser loginAppUser = new LoginAppUser();
        loginAppUser.setAccountId(authInfo.getId());
        loginAppUser.setAccount(authInfo.getAccount());
        loginAppUser.setUserId(sysUser.getId());
        loginAppUser.setUserType(userType.getValue());
        loginAppUser.setLoginTime(LocalDateTime.now());

        Optional.ofNullable(sysRoleManager.getById(sysUser.getRoleId(),EnabledEnum.YES))
                .ifPresent(r -> {
                    Role role = new Role();
                    BeanHelpers.copyProperties(r, role);
                    loginAppUser.setRole(role);
                });

        UserContextManager.login(loginAppUser);
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        AuthenticationResponse authResponse = new AuthenticationResponse();
        authResponse.setToken(tokenInfo.getTokenValue());
        authResponse.setExpiresIn(tokenInfo.getTokenTimeout());
        authResponse.setOpenid(authInfo.getId());
        return authResponse;
    }


}
